<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers;

use App\Filters\InfoFilter;
use App\Http\Requests\InfoNewsRequest;
use App\Http\Requests\InfoRequest;
use App\Http\Resources\InfoNewsCollection;
use App\Http\Resources\InfoNewsResource;
use App\Models\Info;
use App\Models\InfoCategory;
use App\Models\InfoNews;
use App\Services\AttachmentService;
use Illuminate\Support\Facades\DB;

/**
 * 文章模块
 * Class InfoNewsController
 * @package App\Http\Controllers
 */
class InfoNewsController extends Controller
{

    /**
     * 列表
     * @param InfoCategory $info_category
     * @param InfoFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(InfoCategory $info_category, InfoFilter $filter)
    {
        $paginate = Info::where('info.info_category_id', $info_category->id)->filter($filter)->paginate($this->pageSize());
        $result = InfoNewsCollection::make($paginate)->only(['id', 'title', 'recommend_type', 'hits', 'top', 'status', 'created_at', 'updated_at'])->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 查看
     * @param InfoCategory $info_category
     * @param InfoNews $info_news
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(InfoCategory $info_category, InfoNews $info_news)
    {
        $resource = Info::where('info.info_category_id', $info_category->id)->where('info.id', $info_news->info_id)->first();
        if (!$resource) return $this->error(__('common.http_not_found'));
        $result = InfoNewsResource::make($resource)->except(['created_user_name', 'updated_at', 'id', 'hits'])->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param InfoCategory $info_category
     * @param InfoRequest $infoRequest
     * @param InfoNewsRequest $infoNewsRequest
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(InfoCategory $info_category, InfoRequest $infoRequest, InfoNewsRequest $infoNewsRequest)
    {
        $info_data = $infoRequest->validate('store');
        $info_news_data = $infoNewsRequest->validate('store');
        DB::beginTransaction();
        try {
            // 保存info
            $info = new Info($info_data);
            $info->save();
            // 保存info_news
            $info_news = new InfoNews($info_news_data);
            $info_news->info_id = $info->id;
            $info_news->save();
            // 绑定附件
            AttachmentService::bind($info, 'cover_image', $info->cover_image);
            DB::commit();
            return $this->successStored();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorStored();
        }
    }

    /**
     * 修改
     * @param InfoCategory $info_category
     * @param InfoRequest $infoRequest
     * @param InfoNewsRequest $infoNewsRequest
     * @param InfoNews $info_news
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(InfoCategory $info_category, InfoRequest $infoRequest, InfoNewsRequest $infoNewsRequest, InfoNews $info_news)
    {
        $info_data = $infoRequest->validate('update');
        $info_news_data = $infoNewsRequest->validate('update');
        DB::beginTransaction();
        try {
            $info_news->update($info_news_data);
            $info = Info::find($info_news->info_id);
            $info->update($info_data);
            // 绑定附件
            AttachmentService::bind($info, 'cover_image', $info->cover_image);
            DB::commit();
            return $this->successUpdated();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorUpdated();
        }
    }

    /**
     * 更新状态
     * @param InfoCategory $info_category
     * @param InfoRequest $request
     * @param InfoNews $info_news
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(InfoCategory $info_category, InfoRequest $request, InfoNews $info_news)
    {
        $data = $request->validate('updateStatus');
        if (!$info_news->info()->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param InfoCategory $info_category
     * @param $id // 1或者1,2
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(InfoCategory $info_category, $id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            Info::where('info_category_id', $info_category->id)->whereIn('id', $between)->delete();
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 置顶
     * @param InfoCategory $info_category
     * @param InfoRequest $request
     * @param InfoNews $info_news
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateTop(InfoCategory $info_category, InfoRequest $request, InfoNews $info_news)
    {
        $data = $request->validate('updateTop');
        if (!$info_news->info()->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

}
